package com.songcha.module_bookreader.ui.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.songcha.module_bookreader.R
import com.songcha.module_bookreader.bean.QueryWordBean
import com.songcha.library_common.ui.view.loading.FlowerLoadingView
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.dp2px

class QueryWordDialog(context: Context,val mWord:String) : CustomDialog(context) {

    interface IOnWordStarListener{
        fun onWordStar(word:String)
        fun onWordUnStar(word:String)
    }

    private lateinit var mView:View
    //@BindView(R2.id.bookreader_tv)
    lateinit var mTv:TextView
    //@BindView(R2.id.bookreader_flv)
    lateinit var mFlv:FlowerLoadingView
    //@BindView(R2.id.bookreader_iv_error)
    lateinit var mIvError:ImageView
    //@BindView(R2.id.bookreader_iv_star)
    lateinit var mIvStar:ImageView

    private var mListener: IOnWordStarListener?=null

    private var mIsStar=false

    fun setOnWordStarListener(listener: IOnWordStarListener){
        mListener=listener
    }

    fun setIsStar(isStar:Boolean){
        mIsStar=isStar
    }

    override fun getView(root: ViewGroup): View? {
        mView=LayoutInflater.from(context).inflate(R.layout.bookreader_dialog_query_word,null)
        val lp=ConstraintLayout.LayoutParams(0,180.dp2px())
        lp.leftToLeft=ConstraintLayout.LayoutParams.PARENT_ID
        lp.rightToRight=ConstraintLayout.LayoutParams.PARENT_ID
        lp.bottomToBottom=ConstraintLayout.LayoutParams.PARENT_ID
        mView.layoutParams=lp

        mTv=mView.findViewById(R.id.bookreader_tv)
        mFlv=mView.findViewById(R.id.bookreader_flv)
        mIvError=mView.findViewById(R.id.bookreader_iv_error)
        mIvStar=mView.findViewById(R.id.bookreader_iv_star)

        return mView

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        setWidth(ScreenUtil.getScreenWidth())
        setDialogTitle("")
        setGravity(Gravity.BOTTOM)
        setAnim(com.songcha.library_common.R.style.dialogBottomSlideWindowAnim)
        setShowBtn(false)
        setRadius(floatArrayOf(15f.dp2px(),15f.dp2px(),0f,0f))

        super.onCreate(savedInstanceState)

        //ButterKnife.bind(this)

        updateStar()

        mIvStar.setOnClickListener {
            if(mIsStar){
               if(mListener!=null){
                   mListener!!.onWordUnStar(mWord)
               }
            }else{
                if(mListener!=null){
                    mListener!!.onWordStar(mWord)
                }
            }

            val animation=RotateAnimation(-45f,45f,50f,50f)
            animation.duration=200
            animation.repeatCount=3
            animation.repeatMode=Animation.REVERSE
            it.startAnimation(animation)
            animation.setAnimationListener(object : Animation.AnimationListener{
                override fun onAnimationStart(animation: Animation?) {

                }

                override fun onAnimationEnd(animation: Animation?) {
                    mIsStar=!mIsStar
                    updateStar()
                }

                override fun onAnimationRepeat(animation: Animation?) {

                }

            })




        }
    }

    fun updateStar(){
        if(mIsStar){
            mIvStar.setImageResource(R.drawable.bookreader_icon_magic_star)
        }else{
            mIvStar.setImageResource(R.drawable.bookreader_icon_magic_normal)
        }
    }

    fun showWordDetail(bean: QueryWordBean){
        mFlv.visibility=View.GONE
        val sb=StringBuilder()
        for(entry in bean.data.entries){
            sb.append(entry.entry)
            sb.append("\n")
            sb.append(entry.explain)
        }
        mTv.text=sb.toString()
    }

    fun onQueryWordError(){
        mFlv.visibility=View.GONE
        mIvError.visibility=View.VISIBLE
    }









}